/*	WARNING: COPYRIGHT (C) 2017 XSENS TECHNOLOGIES OR SUBSIDIARIES WORLDWIDE. ALL RIGHTS RESERVED.
	THIS FILE AND THE SOURCE CODE IT CONTAINS (AND/OR THE BINARY CODE FILES FOUND IN THE SAME
	FOLDER THAT CONTAINS THIS FILE) AND ALL RELATED SOFTWARE (COLLECTIVELY, "CODE") ARE SUBJECT
	TO A RESTRICTED LICENSE AGREEMENT ("AGREEMENT") BETWEEN XSENS AS LICENSOR AND THE AUTHORIZED
	LICENSEE UNDER THE AGREEMENT. THE CODE MUST BE USED SOLELY WITH XSENS PRODUCTS INCORPORATED
	INTO LICENSEE PRODUCTS IN ACCORDANCE WITH THE AGREEMENT. ANY USE, MODIFICATION, COPYING OR
	DISTRIBUTION OF THE CODE IS STRICTLY PROHIBITED UNLESS EXPRESSLY AUTHORIZED BY THE AGREEMENT.
	IF YOU ARE NOT AN AUTHORIZED USER OF THE CODE IN ACCORDANCE WITH THE AGREEMENT, YOU MUST STOP
	USING OR VIEWING THE CODE NOW, REMOVE ANY COPIES OF THE CODE FROM YOUR COMPUTER AND NOTIFY
	XSENS IMMEDIATELY BY EMAIL TO INFO@XSENS.COM. ANY COPIES OR DERIVATIVES OF THE CODE (IN WHOLE
	OR IN PART) IN SOURCE CODE FORM THAT ARE PERMITTED BY THE AGREEMENT MUST RETAIN THE ABOVE
	COPYRIGHT NOTICE AND THIS PARAGRAPH IN ITS ENTIRETY, AS REQUIRED BY THE AGREEMENT.
*/

#include "xsbaud.h"

/*! \addtogroup cinterface C Interface
	@{
*/

/*! \brief Convert an Xsens baud code to XsBaudRate

	\param baudcode : The code to convert to an XsBaudRate

	\returns An XsBaudrate
*/
XsBaudRate XsBaud_codeToRate(XsBaudCode baudcode)
{
	switch(baudcode)
	{
	case XBC_4k8:		  return XBR_4800;
	case XBC_9k6:		  return XBR_9600;
//	case XBC_14k4:		  return XBR_14k4;
	case XBC_19k2:		  return XBR_19k2;
//	case XBC_28k8:		  return XBR_28k8;
	case XBC_38k4:		  return XBR_38k4;
	case XBC_57k6:		  return XBR_57k6;
//	case XBC_76k8:		  return XBR_76k8;
	case XBC_115k2:		  return XBR_115k2;
	case XBC_230k4:		  return XBR_230k4;
	case XBC_460k8:		  return XBR_460k8;
	case XBC_921k6:
	case XBC_921k6Legacy: return XBR_921k6;
	case XBC_2MegaBaud:	  return XBR_2000k;
	case XBC_3_5MegaBaud: return XBR_3500k;
	case XBC_4MegaBaud:	  return XBR_4000k;
	default:              return XBR_Invalid;
	}
}

/*! \brief Convert a XsBaudRate to an Xsens baud code

	\param baudrate : The code to convert to an XsBaudCode

	\returns An XsBaudCode
*/
XsBaudCode XsBaud_rateToCode(XsBaudRate baudrate)
{
	switch(baudrate)
	{
	case XBR_4800:	return XBC_4k8;
	case XBR_9600:	return XBC_9k6;
//	case XBR_14k4:	return XBC_14k4;
	case XBR_19k2:	return XBC_19k2;
//	case XBR_28k8:	return XBC_28k8;
	case XBR_38k4:	return XBC_38k4;
	case XBR_57k6:	return XBC_57k6;
//	case XBR_76k8:	return XBC_76k8;
	case XBR_115k2:	return XBC_115k2;
	case XBR_230k4:	return XBC_230k4;
	case XBR_460k8:	return XBC_460k8;
	case XBR_921k6:	return XBC_921k6Legacy;
	case XBR_2000k:	return XBC_2MegaBaud;
	case XBR_3500k: return XBC_3_5MegaBaud;
	case XBR_4000k: return XBC_4MegaBaud;
	default:		return XBC_Invalid;
	}
}

/*! \brief Convert a XsBaudrate to a numeric baudrate in bps

	\param baudrate : The XsBaudRate to convert to a numeric baudrate

	\returns A baudrate in bps
*/
int XsBaud_rateToNumeric(XsBaudRate baudrate)
{
	switch(baudrate)
	{
	case XBR_4800:	return 4800;
	case XBR_9600:	return 9600;
//	case XBR_14k4:	return 14400;
	case XBR_19k2:	return 19200;
//	case XBR_28k8:	return 28800;
	case XBR_38k4:	return 38400;
	case XBR_57k6:	return 57600;
//	case XBR_76k8:	return 76800;
	case XBR_115k2:	return 115200;
	case XBR_230k4:	return 230400;
	case XBR_460k8:	return 460800;
	case XBR_921k6:	return 921600;
	case XBR_2000k:	return 2000000;
	case XBR_3500k: return 3500000;
	case XBR_4000k: return 4000000;
	default:		return 0;
	}
}

/*! \brief Convert a numeric baudrate in bps to XsBaudrate

	\param numeric : The numeric baudrate to convert to XsBaudRate

	\returns A XsBaudrate
*/
XsBaudRate XsBaud_numericToRate(int numeric)
{
	switch(numeric)
	{
	case 4800:		return XBR_4800;
	case 9600:		return XBR_9600;
//	case 14400:		return XBR_14k4;
	case 19200:		return XBR_19k2;
//	case 28800:		return XBR_28k8;
	case 38400:		return XBR_38k4;
	case 57600:		return XBR_57k6;
//	case 76800:		return XBR_76k8;
	case 115200:	return XBR_115k2;
	case 230400:	return XBR_230k4;
	case 460800:	return XBR_460k8;
	case 921600:	return XBR_921k6;
	case 2000000:	return XBR_2000k;
	case 3500000:	return XBR_3500k;
	case 4000000:	return XBR_4000k;
	default:		return XBR_Invalid;
	}
}

 /*! @} */ 
